using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace RSSRadio
{
	public partial class DownloadManager : Form
	{
		Timer ProgressTimer;
		int DownloadCount;
		PodcastDownloader2[] Downloaders;

		public DownloadManager()
		{
			InitializeComponent();

			lock (Globals.ActivePodcastDownloaders)
			{
				DownloadCount = Globals.ActivePodcastDownloaders.Count;
				Downloaders = new PodcastDownloader2[DownloadCount];

				DownloadProgressPanel[] downloadpanels = new DownloadProgressPanel[Globals.ActivePodcastDownloaders.Count];
				for (int i = 0; i < Globals.ActivePodcastDownloaders.Count; i++)
				{
					PodcastDownloader2 downloader =
						(PodcastDownloader2)Globals.ActivePodcastDownloaders[i];

					Downloaders[i] = downloader;

					DownloadProgressPanel panel =
						new DownloadProgressPanel(downloader.feed.Title,
						downloader.episode.Title);
					panel.Tag = downloader;
					panel.Dock = DockStyle.Top;
					downloadpanels[i] = panel;

					MenuItem canceler = new MenuItem();
					
					int ordinalIndex = i + 1;
					canceler.Text = ordinalIndex.ToString() + ": " + StringUtil.ChopPhraseDownToLength(downloader.episode.Title, 15);

					canceler.Click += new EventHandler(canceler_Click);
					MnuCancel.MenuItems.Add(canceler);
				}

				// add the download panels to the form in reverse order 
				//		(so they correspond with numbers in the cancel menu)
				for (int i = downloadpanels.Length - 1; i >= 0; i--)
					this.Controls.Add(downloadpanels[i]);
			}

			ProgressTimer = new Timer();
			ProgressTimer.Interval = 1000;
			ProgressTimer.Tick += new EventHandler(ProgressTimer_Tick);
			ProgressTimer.Enabled = true;
		}

		void canceler_Click(object sender, EventArgs e)
		{
			int index = int.Parse(((MenuItem)sender).Text.Substring(0, 1)) - 1;
			Downloaders[index].WantToCancel = true;
		}

		void ProgressTimer_Tick(object sender, EventArgs e)
		{
			foreach (Control control in this.Controls)
				if (control.GetType() == typeof(DownloadProgressPanel))
				{
					DownloadProgressPanel panel = (DownloadProgressPanel)control;

					foreach (PodcastDownloader2 downloader in Globals.ActivePodcastDownloaders)
					{
						if (panel.Tag == downloader)
						{
                            if (downloader.Waiting)
                                panel.LblProgress.Text = "Waiting";
                            else if (downloader.DownloadFailed)
                                panel.LblProgress.Text = "Failed";
                            else if (downloader.WantToCancel)
                            {
                                if (downloader.IsCanceled)
                                    panel.LblProgress.Text = "Canceled";
                                else
                                    panel.LblProgress.Text = "Canceling";
                            }
                            else
                                panel.SetProgress(downloader.PercentCompleted, downloader.KBps, downloader.MegsReceived, downloader.TotalMegs, downloader.SecondsRemaining);
						}
					}
				}
		}

        private void DownloadManager_Activated(object sender, EventArgs e)
        {
            ProgressTimer.Enabled = true;
        }

        private void DownloadManager_Deactivate(object sender, EventArgs e)
        {
            ProgressTimer.Enabled = false;

        }

        private void DownloadManager_Closed(object sender, EventArgs e)
        {
            ProgressTimer.Enabled = false;

        }
	}
}